﻿using Dapper;
using SIG.DAL.Dapper.Model;
using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace SIG.DAL.Dapper.Repositories
{
    public class LinkRepository
    {
        private DbConnection _connection;

        public Link GetById(int id)
        {
            Link result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Get<Link>(id);
            }
            return result;
        }
        public int? Create(Link model)
        {
            int? result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Insert(model);
            }
            return result;
        }

        public int Update(Link model)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Update(model);
            }
            return result;
        }
        public int Delete(Link model)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Delete(model);
            }
            return result;
        }

        public int Delete(int id)
        {
            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.Delete<Link>(id);
            }
            return result;
        }

        public IEnumerable<Link> GetActiveItems()
        {
            IEnumerable<Link> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.GetList<Link>(new { Active = true });
            }
            return result;
        }

        public IEnumerable<Link> GetActiveItems(int parentId)
        {
            IEnumerable<Link> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.GetList<Link>("where Active = 1 and CategoryId = @CategoryId", new { CategoryId = parentId });
            }
            return result;
        }

        /// <summary>
        /// 获取所有文章分页
        /// </summary>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="parentId"></param>
        /// <param name="keyword"></param>
        /// <param name="totalCount"></param>
        /// <returns></returns>
        public IEnumerable<Link> GetListPagedItems(int pageIndex, int pageSize, int parentId,string keyword, out int totalCount)
        {
                           
            IEnumerable<Link> result;
            using (_connection = Utilities.GetOpenConnection())
            {
                keyword = !string.IsNullOrEmpty(keyword) ? keyword.Replace("[", "[[]").Replace("%", "[%]") : keyword;
                string likeKeyword = "%" + keyword + "%";

                var sql = "where 1=1 ";

                if (!string.IsNullOrEmpty(keyword))
                {
                    sql = sql + " and Title like @Keyword ";
                }
                if (parentId > 0)
                {
                    sql = sql + " and CategoryId = @CategoryId ";
                }

                totalCount = _connection.RecordCount<Link>(sql,
                    new { CategoryId = parentId , Keyword = likeKeyword });
                result = _connection.GetListPaged<Link>(pageIndex, pageSize,
                    sql, "Importance desc",
                    new { CategoryId = parentId, Keyword = likeKeyword});
            }
            return result;
        }

        public int GetCountByParentId(int parentId)
        {

            int result;
            using (_connection = Utilities.GetOpenConnection())
            {
                result = _connection.RecordCount<Link>("where CategoryId = @CategoryId", new { CategoryId = parentId });
            }
            return result;
        }

    }

}
